package com.qf.mvc01.ex;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.security.sasl.SaslException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 统一异常处理器
 * 注意：要把自定义的统一异常处理器放入IOC容器才可以生效
 */
public class MyExResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        //1.打印异常
        e.printStackTrace();
        //2.创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        //3.判断异常类型
        if(e instanceof UserException){
            mv.setViewName("redirect:/error/userexception.html");
        }else if(e instanceof SqlException){
            mv.setViewName("redirect:/error/sqlexception.html");
        }else {
            mv.setViewName("/hi.html");
        }
        return mv;
    }
}
